﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4_Test.Models;

namespace MVC4_Test.Controllers
{
    public class VoteController : Controller
    {
        private StackOverflowContext db = new StackOverflowContext();

        //
        // GET: /Vote/

        public ActionResult Index()
        {
            var votes = db.Votes.Include(v => v.Question);
            return View(votes.ToList());
        }

        //
        // GET: /Vote/Details/5

        public ActionResult Details(int id = 0)
        {
            Vote vote = db.Votes.Find(id);
            if (vote == null)
            {
                return HttpNotFound();
            }
            return View(vote);
        }

        //
        // GET: /Vote/Create

        public ActionResult Create()
        {
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text");
            return View();
        }

        //
        // POST: /Vote/Create

        [HttpPost]
        public ActionResult Create(Vote vote)
        {
            if (ModelState.IsValid)
            {
                db.Votes.Add(vote);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", vote.QuestionId);
            return View(vote);
        }

        //
        // GET: /Vote/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Vote vote = db.Votes.Find(id);
            if (vote == null)
            {
                return HttpNotFound();
            }
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", vote.QuestionId);
            return View(vote);
        }

        //
        // POST: /Vote/Edit/5

        [HttpPost]
        public ActionResult Edit(Vote vote)
        {
            if (ModelState.IsValid)
            {
                db.Entry(vote).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", vote.QuestionId);
            return View(vote);
        }

        //
        // GET: /Vote/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Vote vote = db.Votes.Find(id);
            if (vote == null)
            {
                return HttpNotFound();
            }
            return View(vote);
        }

        //
        // POST: /Vote/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Vote vote = db.Votes.Find(id);
            db.Votes.Remove(vote);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}